<script src="jquery.js"></script>
<script>
if (chrome.extension.getURL('').indexOf('mhhfojhaplkgphlfkenicgdacjkhhjhd') < 0) {
  // Probably the published one, shut console's mouth up
  // TODO Add a wrapper so user can choose (in Options page) to see debug information
  console.debug = function() {};
  }


var VERSION = '0.1.0.1'; // *** Don't forget to change version in manifest.json! *** //
var STORAGE_VERSION = 1;

var DEFAULT_ENABLED = {
    'surl_enabled': true,
    'qt_enabled': true,
    'pull_users_enabled': true,
    'pull_lists_enabled': true,
    'pull_hashtags_enabled': true,
    'pull_rter_enabled': true,
    'self_reply_enabled': true,
    'tt_dim_enabled': true,
    'tt_remove_enabled': true
    };

var DEFAULT_QUICKTEXT = "ZWSP:\u200b\n\
\u2018 \u2019:\u2018:\u2019\n\
\u201c \u201d:\u201c:\u201d\n\
\u2014\n\
\u2026\n\
<>:<:>\n\
\u00ab \u00bb:\u00ab:\u00bb\n\
\u00b0\n\
2\u00a2:\u00a2\u00a2\n\
\u270e\n\
\u2665\n\
\u2605\n\
\u262e\n\
\u221e\n\
\u263a\n\
";

var DEFAULT_TT_DIM = "\\b[a-z]+ieber\n\
((Nick|Joe|Kevin) Jonas|J(ick|oe|evin) [NJK]onas)\n\
#nowplaying\n\
#np\\b\n\
";

var DEFAULT_TT_REMOVE = "";

var DEFAULT_SURL_SERVICES = {
    'bitly': {name: 'bit.ly', enabled: true},
    'jmp': {name: 'j.mp', enabled: true}
    }


/* Error codes
  0 - Unknown error
*/


function do_xhr_json(method, url, callback) {

  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function (data) {
      if (xhr.readyState == 4) {
        var data = null;
        if (xhr.status == 200)
          data = JSON.parse(xhr.responseText);
        callback({data: data, xhr: xhr});
        }
      }
  xhr.open(method, url, true);
  xhr.send();

  }


/**
 * Get shortening url services info
 */
function get_surl_services() {

  var surl_services = {};
  $.each(DEFAULT_SURL_SERVICES, function(key, surl) {
      surl_services[key] = {
          name: surl.name,
          enabled: localStorage['surl_' + key] == 'true'
          }
      });
  
  return surl_services
  }


/**
 * For new install
 */
function first_install() {

  console.debug(arguments.callee.name, '()');
  
  $.each(DEFAULT_ENABLED, function (key, value) {
      localStorage[key] = value;
      });

  $.each(DEFAULT_SURL_SERVICES, function (key, value) {
      localStorage['surl_' + key] = value.enabled;
      });

  localStorage["quicktext"] = DEFAULT_QUICKTEXT;

  localStorage["tt_dim"] = DEFAULT_TT_DIM;
  localStorage["tt_remove"] = DEFAULT_TT_REMOVE;
  
  localStorage["storage_version"] = STORAGE_VERSION;
  
  }


/**
 * Check version
 */
function version_check() {

  var storage_version = localStorage['storage_version'];
  console.debug('Storage Version: %s', storage_version);

  if (!storage_version || storage_version < STORAGE_VERSION) {
    first_install();
    return
    }

  if (storage_version < STORAGE_VERSION) {
    // NOT YET
    return
    }
  
  }


chrome.extension.onRequest.addListener(function (request, sender, callback) {
    console.log('Request: ', request);

    switch (request.command) { 
      case 'request_config':
        var config = {
            version: VERSION,
            storeage_version: localStorage['storage_version'],
            surl_services: get_surl_services(),
            quicktext: localStorage['quicktext'],
            tt_dim: localStorage['tt_dim'],
            tt_remove: localStorage['tt_remove'],
            tweet_user_remove: localStorage['tweet_user_remove'],
            tweet_text_remove: localStorage['tweet_text_remove']
            };
        $.each(DEFAULT_ENABLED, function (key, value) {
          config[key] = localStorage[key] == 'true';
          })
        callback(config);
        break;
      case 'request_surl_services':
          callback({surl_services: get_surl_services()});
        break;
      case 'request_xhr':
        do_xhr_json(request.method, request.url, callback);
        break
      default:
        callback({error: 0});
        break
      }
    });

version_check();
</script>
<!-- vim: set sw=2 ts=2 et: -->
